﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySQLStudio.BusinessLogic;
using MySQLStudio.Common;

namespace MySQLStudio.GUI.Controls.StoreProcedure
{
    class RoutineActivitiesScriptWindow : ScriptWindow
    {
        private RoutineFormType m_formType;
        private StoredProcedure m_storedProcedure;


        public override String SqlStatement
        {
            get { return (m_SqlStatement = ScriptTextBox.Text); }
            set { m_SqlStatement = value; }
        }

        public RoutineFormType FormType
        {
            get { return m_formType; }
            set { m_formType = value; }
        }

        public StoredProcedure StoredProcedure
        {
            get { return m_storedProcedure; }
            set { m_storedProcedure = value; }
        }

        public RoutineActivitiesScriptWindow(Host host, Database database)
            : base(host, database)
        {
            
        }

        public void AlterRoutineStatement()
        {
            if (this.m_storedProcedure != null)
            {
                this.Text = string.Format("{0} on {1}.{2}", this.m_storedProcedure != null ? this.m_storedProcedure.Name : string.Empty,
                                                            this.Database != null ? this.Database.Name : string.Empty,
                                                            this.Host != null ? this.Host.Hostname : string.Empty);
                ScriptTextBox.Text = this.m_storedProcedure.GetAlterStatement(this.Host.ConnectionString);
                ScriptTextBox.OnTextChanged();
            }
        }

        public void CreateNewRoutineStatement()
        {
            this.Text = string.Format("script on {0}.{1}", this.Database != null ? this.Database.Name : string.Empty,
                                                           this.Host != null ? this.Host.Hostname : string.Empty);
            ScriptTextBox.Text = InformationSchemaHelper.GetCreateStoredProcedureStatementCommand();
            ScriptTextBox.OnTextChanged();
        }

        private string GenerateCreateRoutineStatement(Database databaseContext, string createRoutineSQL)
        {
            return string.Format("USE {0};\n{1}", databaseContext.Name, createRoutineSQL);
        }

    }
}
